const { getMsgList, getMsgContentList, getMsgServiceList} = require('../model/msg');
const { redisGet } = require('../utils/redis');

// 获取聊天记录列表
const getMsgListController = async (req, res) => {
    try {
        const { user, toUser, page, pageSize } = req.query;
        const userInfo = await redisGet(`mim-${user}`);
        const toUserInfo = await redisGet(`mim-${toUser}`);
        let result = []
        if(userInfo.userType == 3 || toUserInfo.userType == 3){
            const getUser = userInfo.userType == 3?toUser:user
            result = await getMsgServiceList({ getUser, page, pageSize });
        }else{
            result = await getMsgList({ user, toUser, page, pageSize });
        }
        res.json({
            code: 200,
            message: '获取成功',
            data: result
        });
    } catch (e) {
        res.json({
            code: 500,
            message: e.message
        });
    }
}

// 分页查询包含敏感词的消息列表 
const getMsgContentListController = async (req, res) => {
    try {
        const { keyWord, page, pageSize } = req.query;
        const result = await getMsgContentList({ keyWord, page, pageSize });
        console.log(result)
        res.json({
            code: 200,
            message: '获取成功',
            data: {
                list: result.list,
                total: result.total,
                totalPages: result.totalPages
            }
        });
    } catch (e) {
        res.json({
            code: 500,
            message: e.message
        });
    }
}

module.exports = {
    getMsgListController,
    getMsgContentListController
}